﻿using Best.SmartCms.Core.Utility;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace Best.SmartCms.Utility
{
    public class HttpRequestHelper
    {
        /// <summary>
        /// 静态httpclient
        /// </summary>
        private static readonly HttpClient HttpDefaultClient;

        /// <summary>
        /// 默认静态httpclient
        /// </summary>
        static HttpRequestHelper()
        {
            HttpDefaultClient = new HttpClient { Timeout = TimeSpan.FromSeconds(15) };
            HttpDefaultClient.DefaultRequestHeaders.Add("KeepAlive", "false");
        }

        /// <summary>
        /// HttpClient POST
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static async Task<string> SendPost(string url, HttpContent data)
        {
            NLogManager.LogTrace($"请求平台SendPost:{url}");
            var client = HttpDefaultClient;
            var response = await client.PostAsync(url, data);
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// HttpClient POST
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Task<HttpResponseMessage> ApiPost(string url, HttpContent data = null)
        {
            NLogManager.LogTrace($"请求平台ApiPost:{url}");
            var client = HttpDefaultClient;
            return client.PostAsync(url, data);
        }



        /// <summary>
        /// Get
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static Task<HttpResponseMessage> ApiGet(string url)
        {
            NLogManager.LogTrace($"请求平台ApiGet:{url}");
            var client = HttpDefaultClient;
            return client.GetAsync(url);
        }

        
       

        /// <summary>
        /// 
        /// </summary>
        /// <param name="timeoutMilliseconds"></param>
        /// <returns></returns>
        public static HttpClient CreateHttpClient(double? timeoutMilliseconds = null)
        {
            var defaulHttpClientHandler = new HttpClientHandler
            {
                AllowAutoRedirect = true,
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                // allow concurrent request
                MaxConnectionsPerServer = 2000,
                // allow all ssl cert
                ServerCertificateCustomValidationCallback = (message, certificate2, sslPolicyError, arg4) => true,
                UseCookies = false,
                UseProxy = false,
                Proxy = null
            };

            var httpClient = new HttpClient(defaulHttpClientHandler);

            if (timeoutMilliseconds.HasValue) httpClient.Timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds.Value);
            httpClient.DefaultRequestHeaders.ConnectionClose = false;
            httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true };
            httpClient.MaxResponseContentBufferSize = 1024 * 1024 * 1024;

            return httpClient;
        }
    }
}
